﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "Anim/CharacterAnimInstance.h"
#include "Character/PlayerCharacter.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "KismetAnimationLibrary.h"
#include "Helper/DebugHelper.h"

void UCharacterAnimInstance::NativeInitializeAnimation()
{
	// 获得PawnOwner
	APawn* GetPawnOwner = TryGetPawnOwner();
	Owner = Cast<ABaseCharacter>(GetPawnOwner);
}

void UCharacterAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
{
	if (Owner)
	{
		// 计算速度大小
		Speed_Anim = Owner->GetVelocity().Length();
		FRotator ActorRotation = Owner->GetActorRotation();

		// 计算动画速度（pawn的方向和移动的方向）
		Direction_Anim = UKismetAnimationLibrary::CalculateDirection(Owner->GetVelocity(), ActorRotation);

		// 是否下落中 用owner的移动组件获取
		Falling_Anim = Owner->GetCharacterMovement()->IsFalling();

		//是否死亡
		IsDeath_Anim = Owner->IsDeath_Anim;

		//是否攻击状态 pawn的攻击状态
		APlayerCharacter* player = Cast<APlayerCharacter>(Owner);
		if (player)
		{
			IsAttack_Anim = player->IsAttackState_Anim;
		}
	}
}

void UCharacterAnimInstance::AnimNotify_WeaponToHand(UAnimNotify* AnimNotify)
{
	//Debug::printLog("AnimNotify_WeaponToHand");
	APlayerCharacter* player = Cast<APlayerCharacter>(Owner);
	if (player)
	{
		player->ChangeWeaponAnimSocketName(player->Weapon, FName("WeaponToHandPoint"));
	}
}

void UCharacterAnimInstance::AnimNotify_ChangeWeaponPose(UAnimNotify* AnimNotify)
{
	//Debug::printLog("AnimNotify_ChangeWeaponPose");
	APlayerCharacter* player = Cast<APlayerCharacter>(Owner);
	if (player)
	{
		player->ChangeWeaponAnimSocketName(player->Weapon, FName("AttackWeaponPoint"));
	}
}

void UCharacterAnimInstance::AnimNotify_UnEquipToNormal(UAnimNotify* AnimNotify)
{
	//Debug::printLog("AnimNotify_UnEquipToNormal");
	//放回背上
	APlayerCharacter* player = Cast<APlayerCharacter>(Owner);
	if (player)
	{
		player->ChangeWeaponAnimSocketName(player->Weapon, FName("NormalWeaponSocket"));
	}
}

void UCharacterAnimInstance::AnimNotify_UnEquipEnd(UAnimNotify* AnimNotify)
{
	//Debug::printLog("AnimNotify_UnEquipEnd");
	//APlayerCharacter* player = Cast<APlayerCharacter>(Owner);
	if (Owner)
	{
		Owner->SetMovementMode(MOVE_Walking);
	}
}

void UCharacterAnimInstance::AnimNotify_EquipEnd(UAnimNotify* AnimNotify)
{
	//APlayerCharacter* player = Cast<APlayerCharacter>(Owner);
	if (Owner)
	{
		Owner->SetMovementMode(MOVE_Walking);
	}
}

void UCharacterAnimInstance::AnimNotify_AttackEnd(UAnimNotify* AnimNotify)
{
	//APlayerCharacter* player = Cast<APlayerCharacter>(Owner);
	if (Owner)
	{
		Owner->SetMovementMode(MOVE_Walking);
	}
}

void UCharacterAnimInstance::AnimNotify_EnemyAttackEnd(UAnimNotify* AnimNotify)
{
	ABaseCharacter* BaseCharacter = Cast<ABaseCharacter>(Owner);
	//Debug::printLog("EnemyAttackEnd");
	if (BaseCharacter)
	{
		BaseCharacter->IsAttacking_AI = false;
	}
}

void UCharacterAnimInstance::AnimNotify_AttackDamage(UAnimNotify* AnimNotify)
{
	if (Owner)
	{
		Owner->AttackDamage();
	}
}
